package com.java.util.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import com.java.util.MyCons;
import com.java.util.servlet.ServletUtil;

@Configuration
public class UserInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Object user = request.getSession().getAttribute(MyCons.USER_ROLE);
		if (null == user) {
			if(ServletUtil.isAjax(request)) {
				ServletUtil.noLogin(response);
			}else {
				request.getRequestDispatcher("/user/login").forward(request, response);
			}
			return false;
		}
		return true;
	}
}